package ilender.typemappers;

import java.sql.*;
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;

import org.apache.ibatis.type.*;

import ilender.util.*;

/**Used for converting lists to PostgreSQL arrays and vice versa.*/
public class ListTypeHandler extends BaseTypeHandler<List>{

	public void setNonNullParameter(PreparedStatement ps,int i,List parameter,JdbcType jdbcType) throws SQLException{
		ps.setArray(i,ps.getConnection().createArrayOf("varchar",parameter.toArray(new String[0])));
	}

	public List getNullableResult(ResultSet rs, String columnName) throws SQLException{
		Array array=rs.getArray(columnName);
		if(null!=array){
			return Arrays.asList((Object[])(array).getArray());
		}
		else
			return new ArrayList();
	}

	public List getNullableResult(ResultSet rs, int columnIndex) throws SQLException{
		Array array=rs.getArray(columnIndex);
		if(null!=array){
			return Arrays.asList((Object[])(array).getArray());
		}
		else
			return new ArrayList();
	}

	public List getNullableResult(CallableStatement cs, int columnIndex) throws SQLException{
		Array array=cs.getArray(columnIndex);
		if(null!=array){
			return Arrays.asList((Object[])(array).getArray());
		}
		else
			return new ArrayList();
	}
}
